package com.nuo.quartz.serve.controller;

import com.nuo.common.core.page.PageResult;
import com.nuo.common.core.result.Result;
import com.nuo.quartz.beans.pojo.request.QuartzJobAddReqDTO;
import com.nuo.quartz.beans.pojo.request.QuartzJobPageQueryReqDTO;
import com.nuo.quartz.beans.pojo.request.QuartzJobUpdateReqDTO;
import com.nuo.quartz.beans.pojo.request.QuartzLogPageQueryReqDTO;
import com.nuo.quartz.beans.pojo.response.QuartzJobRespDTO;
import com.nuo.quartz.beans.pojo.response.QuartzLogRespDTO;
import com.nuo.quartz.serve.service.QuartzJobDOService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 定时任务管理
 *
 * @author LGC
 */
@Api(tags = "定时任务管理")
@RestController
@RequestMapping(value = "/quartz")
public class QuartzController {

    @Resource
    private QuartzJobDOService quartzJobDOService;

    @ApiOperation("查询任务")
    @GetMapping("/job/{id}")
    public Result<QuartzJobRespDTO> getById(@PathVariable Integer id) {
        return Result.success(quartzJobDOService.getById(id));
    }

    @ApiOperation("任务分页查询")
    @PostMapping("/job/pageList")
    public Result<PageResult<QuartzJobRespDTO>> jobPage(@RequestBody QuartzJobPageQueryReqDTO dto) {
        return Result.success(quartzJobDOService.jobPage(dto));
    }

    @ApiOperation("新增任务")
    @PostMapping("/job")
    public Result<Integer> add(@RequestBody QuartzJobAddReqDTO dto) {
        return Result.success(quartzJobDOService.add(dto));
    }

    @ApiOperation("更新任务")
    @PutMapping("/job")
    public Result<Integer> update(@RequestBody QuartzJobUpdateReqDTO dto) {
        return Result.success(quartzJobDOService.update(dto));
    }

    @ApiOperation("暂停任务")
    @PutMapping("/job/pause/{id}")
    public Result<Void> pause(@PathVariable("id") Integer id) {
        quartzJobDOService.pause(id);
        return Result.success();
    }

    @ApiOperation("恢复任务")
    @PutMapping("/job/resume/{id}")
    public Result<Void> resume(@PathVariable("id") Integer id) {
        quartzJobDOService.resume(id);
        return Result.success();
    }

    @ApiOperation("执行一次任务")
    @GetMapping("/job/runOnce/{id}")
    public Result<Void> runOnce(@PathVariable("id") Integer id) {
        quartzJobDOService.runOnce(id);
        return Result.success();
    }

    @ApiOperation("任务执行日志分页查询")
    @PostMapping("/job/jobLogPage")
    public Result<PageResult<QuartzLogRespDTO>> jobLogPage(@RequestBody QuartzLogPageQueryReqDTO dto) {
        return Result.success(quartzJobDOService.jobLogPage(dto));
    }
}
